package com.frxuexi.park2209.park

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.frxuexi.base.db.ParkDao
import com.frxuexi.base.entity.ParkListItem
import com.frxuexi.base.mvvm.BaseViewModel
import com.frxuexi.base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import javax.inject.Inject

@HiltViewModel
class ParkViewModel @Inject constructor(val apiService: ApiService,val parkDao: ParkDao):BaseViewModel() {

    val parksSuccess = MutableStateFlow<String>("")
    val parksSucRoom = MutableLiveData<List<ParkListItem>>()

    val colorSuccess = MutableStateFlow<String>("")

    val addParkSuccess=MutableStateFlow<String>("")
    val addParkSucRoom=MutableStateFlow<Long>(-1)

    val deleteParkSuccess=MutableStateFlow<String>("")
    val updateParkSuccess=MutableStateFlow<String>("")
    val updateParkSucRoom=MutableStateFlow<Int>(-1)

    fun getAllParks(){
        httpRequest({apiService.getAllParks()},{
            parksSuccess.value=it
        })
    }

    fun getParks(){
        roomRequest({parkDao.getParks()},{
            parksSucRoom.value=it
        })
    }

    fun getColors(){
        httpRequest({apiService.getColors()},{colorSuccess.value=it})
    }

    fun addPark(map: Map<String,Any>){
        httpRequest({apiService.addPark(mapToBody(map))},{
            addParkSuccess.value=it
        })
    }

    fun addPark(parkListItem: ParkListItem){
        roomRequest({parkDao.addPark(parkListItem)},{addParkSucRoom.value=it})
    }

    fun deletePark(map: Map<String, Int>){
        httpRequest({apiService.deletePark(mapToBody(map))},{
            deleteParkSuccess.value=it+map.get("parkId")
        })
    }

    fun updatePark(map: Map<String, Any>){
        httpRequest({apiService.updatePark(mapToBody(map))},{updateParkSuccess.value=it})
    }

    fun updatePark(parkListItem: ParkListItem){
        roomRequest({parkDao.updatePark(parkListItem)},{
            updateParkSucRoom.value=it+parkListItem.parkId!!
        })
    }


}